package com.aptemo.webetek.client.widgets.stakes;

import java.util.Date;
import java.util.MissingResourceException;

import com.aptemo.webetek.client.Webetek;
import com.aptemo.webetek.client.enums.EnumBetStatus;
import com.aptemo.webetek.client.widgets.CurrencyWidget;
import com.aptemo.webetek.client.widgets.DateTimeWidget;
import com.aptemo.webetek.client.widgets.NumberWidget;
import com.aptemo.webetek.shared.BetBean;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class StakeRow
		extends FocusPanel implements StakeRowPresenter.Display
{

	private static StakeRowUiBinder	uiBinder	= GWT.create(StakeRowUiBinder.class);

	interface StakeRowUiBinder extends UiBinder<Widget, StakeRow>
	{
	}

	@UiField
	VerticalPanel	betPicks;

	@UiField
	DateTimeWidget	dateField;

	@UiFactory
	DateTimeWidget createDateTimeWidget()
	{
		return new DateTimeWidget(DateTimeFormat.getShortDateTimeFormat());
	}

	@UiField
	CurrencyWidget	amountField;

	@UiField
	NumberWidget	oddsField;

//	@UiField
//	Label			statusField;

	@UiField
	Label			winningsLabel;

	@UiField
	CurrencyWidget	winningsField;

	public StakeRow(BetBean stake)
	{
		add(uiBinder.createAndBindUi(this));

		// double pickwin = 0.0;
		// for (PickBean pick : stake.getPicks())
		// {
		// //GameRow row = new GameRow(pick.getBet());
		// if ((pick.getStatus() == EnumPickStatus.WON))
		// {
		// pickwin += stake.getAmount() * pick.getOdds();
		// }
		//
		// //double odds = stake.getOddsSum();
		// }

		Date date = stake.getDate();
		getStakeDateField().setValue(date);
		getStakeAmount().setValue(stake.getAmount());
		getOddsField().setValue(stake.getOddsSum());

		if (stake.getStatus() == EnumBetStatus.CLOSED)
		{
			getWinningsField().setValue(stake.getEarnings());
			winningsLabel.setText(Webetek.getConstants().earnings());
		}
		else
		{
			getWinningsField().setValue(stake.getOddsSum() * stake.getAmount());
			winningsLabel.setText(Webetek.getConstants().possibleWinnings());
		}

//		try
//		{
//			String trans = Webetek.getConstants().getString(stake.getStatus().toString());
//			getStatusField().setText(trans);
//		}
//		catch (MissingResourceException e)
//		{
//			getStatusField().setText(stake.getStatus().toString());
//		}

	}

	@Override
	public Widget getDisplay()
	{
		return this;
	}

	@Override
	public HasValue<Date> getStakeDateField()
	{
		return dateField;
	}

	@Override
	public HasValue<Double> getStakeAmount()
	{
		return amountField;
	}

	@Override
	public HasValue<Double> getOddsField()
	{
		return oddsField;
	}

//	@Override
//	public HasText getStatusField()
//	{
//		return statusField;
//	}

	@Override
	public HasValue<Double> getWinningsField()
	{
		return winningsField;
	}
}
